package ru.usu.gv.uicomponents.viewitems.vertex;

import com.sun.j3d.utils.geometry.Sphere;

import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.UIElementBase;
import ru.usu.gv.uicomponents.viewitems.ViewItemBase;
import ru.usu.gv.utils.geometry.Polyline;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;

import java.awt.Color;

/**
 * @author spupyrev
 * 04.11.2008
 */
public class SphereView extends ViewItemBase implements IVertexViewItem
{
	private Sphere sphere;

	public SphereView(Appearance appearance, TransformGroup tg)
	{
		super(tg, appearance, appearance.getMaterial());

		float normalSize = (float) StyleFacade.getVertexSize();
		sphere = new Sphere(normalSize, Sphere.GENERATE_NORMALS, 50, appearance);
		tg.addChild(sphere);
	}

	public void makePickable(UIElementBase ge)
	{
		ge.makePickable(sphere.getShape());
	}

	@Override
	public void setColor(Color color)
	{
		super.setColor(color);
		getMaterial().setDiffuseColor(new Color3f(color));
	}

	public Polyline getBoundingPolygon()
	{
		BoundingSphere bounds = (BoundingSphere) sphere.getBounds();
		bounds.transform(getTransform());

		Polyline polyline = new Polyline();
		Point3d center = new Point3d();
		bounds.getCenter(center);
		double radius = bounds.getRadius();
		polyline.addPoint(new Point2d(center.x - radius, center.y - radius));
		polyline.addPoint(new Point2d(center.x + radius, center.y - radius));
		polyline.addPoint(new Point2d(center.x + radius, center.y + radius));
		polyline.addPoint(new Point2d(center.x - radius, center.y + radius));
		return polyline;
	}

}
